package com.kgis.fm.business.access.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;

import com.kgis.fm.access.common.service.impl.AbstractDataAccessServiceImpl;
import com.kgis.fm.access.metadata.service.ComponentAccessService;
import com.kgis.fm.business.access.service.FmEntityAccessService;
import com.kgis.fm.business.access.support.FMDBConst;
import com.kgis.fm.business.model.Entity;
import com.kgis.fm.model.metadata.Component;

public class FmEntityAccessServiceImpl extends AbstractDataAccessServiceImpl implements FmEntityAccessService<Entity> {
	private static final transient Log log = LogFactory.getLog(FmEntityAccessServiceImpl.class);

	private ComponentAccessService componentAccessService;
	public ComponentAccessService getComponentAccessService() {
		return componentAccessService;
	}

	public void setComponentAccessService(ComponentAccessService componentAccessService) {
		this.componentAccessService = componentAccessService;
	}

	@Override
	public long sequence(String sequenceName) {
		long sequenceValue = this.getJdbcTemplateDataAccessObject().sequence(sequenceName);
		return sequenceValue;
	}

	@Override
	public List<Entity> findByIds(long[] ids) {
		return null;
	}

	@Override
	public Entity create(Entity entity) {
		if(entity.getFno()==0 || entity.getCno()==0 || entity.getFid()==0){
			if(log.isErrorEnabled()){
				log.error("fno,cno,fid必须设置值");
			}
			return null;
		}
		
		List<Object> args = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		Component component = componentAccessService.findById(Long.parseLong(entity.getCno() + ""));
		if(null==component){
			if(log.isErrorEnabled()){
				log.error("根据cno"+entity.getCno()+"找不到对应的component信息");
			}
			return null;
		}
		sql.append(" insert into " + component.getTableName() + " (" + FMDBConst.KGM_FNO + "," + FMDBConst.KGM_CNO+","+FMDBConst.KGM_FID);
		args.add(entity.getFno());
		args.add(entity.getCno());
		args.add(entity.getFid());
		if (entity.getAttributes() != null && entity.getAttributes().size() > 0) {
			for (String field : entity.getAttributes().keySet()) {
				if (!FMDBConst.KGM_FNO.equals(field) && !FMDBConst.KGM_CNO.equals(field)) {
					sql.append("," + field);
					if (entity.getAttributes().get(field) instanceof Date) {
						String obj = "to_date(" + DateFormatUtils.format((Date) entity.getAttributes().get(field), "yyyy-mm-dd") + ")";
						args.add(obj);
					} else {
						args.add(entity.getAttributes().get(field));
					}
				}
			}
		}
		sql.append(") values (");
		sql.append("?");
		for (int i = 1; i < args.size(); i++) {
			sql.append(", ?");
		}
		sql.append(")");
		try {
			int flag = this.getJdbcTemplateDataAccessObject().getJdbcTemplate().update(sql.toString(), args.toArray());
			if (flag > 0) {
				return entity;
			}
		} catch (DataAccessException e) {
			if(log.isErrorEnabled()){
				log.error("insert entity"+entity.toString()+" is error "+e.getMessage());
			}
			return null;
		}
		
		return null;
	}

	@Override
	public Entity remove(Entity entity) {
		return null;
	}

	@Override
	public Entity update(Entity entity) {
		return null;
	}

	@Override
	public List<Entity> findAll() {
		return null;
	}

	@Override
	public Entity findById(long id) {
		return null;
	}

	@Override
	public Entity save(Entity model) {
		return null;
	}

	@Override
	public Entity createGeometryEntity(Entity entity) {
//		spatialDataManagementService.
		return null;
	}

}
